import { Shop } from 'shop-model.js';
var shop = new Shop();
Page({
  data: {
    btnType: 'disable',
    money:0,
    btnText: "确认提现",
    onLoading: false,
  },
  onLoad: function (options) {
    this.getInfo();
  },
  onReady: function () {

  },
  onShow: function () {

  },
  //按钮状态变化
  moneyChange: function (e) {
    var val = e.detail.value;
    this.setData({ money:val });
    let type = 'disable';
    if (val && val > 0) {
      type = 'btn'
    }
    this.setData({
      btnType: type
    })
  },
  //获取可提现余额
  getInfo: function () {
    var that = this;
    var args = {}
    shop.record(args, (data) => {
      that.setData({
        withdrawable: data.withdrawable
      })
    });
  },
  //申请提现
  withdraw:function(e){
    if(this.data.money <= 0){
      wx.showToast({
        title: '请填写正确的金额',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(this.data.onLoading){
      return false;
    }
    this.data.onLoading = true;
    this.setData({btnText:'正在提现...'})
    var that = this;
    var args = { "money": this.data.money, "client": "wxMini"}
    shop.withdrawal(args, (data) => {
      that.data.onLoading = false;
      this.setData({ btnText: '确认提现' })
      if(data.status == 1){
        wx.showToast({
          title: '申请提现成功，请耐心等待管理员审核！',
          icon: 'none',
          duration: 3000,
        })
        setTimeout(function () {
            wx.navigateTo({
              url: 'wallet',
            })
        },2500)
      }else{
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  }
})